/**
 * 初始化数据
 * @param {Object} type
 * @param {Object} id
 */
function initData(type,id){
	if(type=="update"){
		$("#b_url").val("/rest/BaseUserBusiness/modify");
		$$.ajax({
	        url:"/rest/BaseUserBusiness/query",
	        data : {
	            id : id
	        },
	        success : function(data){
	            $("#b_id").val(data.id);
	            $("#b_userName").val(data.userName);
	            $("#b_userCode").val(data.userCode);
	            $("#b_userCode").attr("disabled",true);
	            $("#b_pwd").val(data.pwd);
	            $("#b_mobile").val(data.mobile);
	            $("#b_email").val(data.email);
	            $("#b_rowSort").val(data.rowSort);
	            var status = data.status;
	            if(status==1){
	            	$('#in-use').attr('checked', 'checked');
	            }else{
	            	$('#not-in-use').attr('checked', 'checked');
	            }
	            $("#b_remarks").val(data.remarks);
	        }
	    });
	}else{
		$("#b_url").val("/rest/BaseUserBusiness/add");
	}
}


// 手机号码验证  
jQuery.validator.addMethod("isMobile", function(value, element) {  
    var length = value.length;  
    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;  
    return this.optional(element) || (length == 11 && mobile.test(value));  
}, "请正确填写您的手机号码");


/**
 * 校验表单
 */
function validForm(){
	return $("#userForm").valid();
}

/**
 * 添加表单校验
 */
function menuFormValid() {
    $("#userForm").validate({
        rules: {
            b_userName: {
                required: true,
                rangelength: [1,30]
            },
            b_userCode: {
                required: true,
                rangelength: [1,30]
            },
            b_pwd: {
                required: true,
                rangelength: [1,30]
            },
            b_mobile: {
                required: true,
                isMobile: true
            },
            b_email: {
            	maxlength:30,
                email:true
            },
            b_rowSort:{
                required: true,
                range: [1,9999999]
            },
            b_remarks:{
                maxlength: 250
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips",
        success: 'valid',
        messages: {
            b_userName: {
                required: "(必填)",
                rangelength:"(用户姓名必须是{0}到{1}个字符)"
            },
            b_userCode: {
                required: "(必填)",
                rangelength:"(登录账号必须是{0}到{1}个字符)"
            },
            b_pwd: {
                required: "(必填)",
                rangelength:"(登录密码必须是{0}到{1}个字符)"
            },
            b_mobile: {
                required: "(必填)",
                isMobile: "(请正确填写您的手机号码)"
            },
            b_email: {
            	maxlength:"(最多输入{0}个字符)",
                email:"(请输入正确的邮箱地址)"
            },
            b_rowSort: {
                required: "(必填)",
                range:"(排序值必须在{0}和{1}之间)"
            },
            b_remarks: {
                maxlength:"(备注不能超过250个字符)"
            }
        },
    });
}

/**
 * 执行数据保存
 */
function submitForm() {
	var validFormRes = false;
    $$.form({
    	fname:"#userForm",
        async:false,
        able:function(){
            validFormRes = false;
        },
        checkForm:validForm,
        success:function () {
            validFormRes = true;
        }
    });
    return validFormRes;
}

$(function(){
	$('#b_userName').focus();
    menuFormValid();
})